package com.nt.train.controller;
import com.nt.train.entity.Dict;
import com.nt.train.service.DictService;
import com.nt.train.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * 模拟开发字典增删改查列表处理
 * @author hlf
 * @version 1.0
 * @date 2022/8/17 17:16
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @PostMapping(value = "/listDictPage/{pageIndex}/{pageSize}")
    public AjaxResult listDictPage(@PathVariable Integer pageIndex,@PathVariable Integer pageSize, @RequestBody  Dict dict) {
        List<Dict>  pageList = dictService.listDictPage(pageIndex,pageSize,dict);
        return AjaxResult.success(pageList);
    }

    @PostMapping(value = "/findList")
    public AjaxResult findList(@RequestBody  Dict dict) {
        List<Dict>  pageList = dictService.selectDictAll();
        return AjaxResult.success(pageList);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return AjaxResult.success(dictService.selectDictById(id));
    }

    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody Dict dict) {
        Dict d = dictService.getByCode(dict.getCode());
        if (d != null) {
            return AjaxResult.error("字典码({code})已被使用".replace("{code}", dict.getCode()));
        }
        dictService.insertDict(dict);
        return AjaxResult.success();
    }

    @PostMapping(value = "/edit")
    public AjaxResult edit(@RequestBody Dict dict) {
        Dict d = dictService.getByCode(dict.getCode());
        if (d != null) {
            return AjaxResult.error("字典码({code})已被使用".replace("{code}", dict.getCode()));
        }
        dictService.updateDict(dict);
        return AjaxResult.success();
    }

    @PostMapping(value = "/del/{id}")
    public AjaxResult del(@PathVariable Long id) {
        dictService.deleteDictById(id);
        return AjaxResult.success();
    }

    @PostMapping(value = "/batchdel")
    public AjaxResult batchdel(@RequestBody List<Long> ids) {
        dictService.deleteDictByIds(ids);
        return AjaxResult.success();
    }

    //http://localhost:8080/dict/batchdel2?ids=1,2,3
    @PostMapping(value = "/batchdel2")
    public AjaxResult batchdel2(@RequestParam("ids") List<Long> ids) {
        dictService.deleteDictByIds(ids);
        return AjaxResult.success();
    }

    //http://localhost:8080/dict/batchdel2?ids=1,2,3
    @PostMapping(value = "/batchdel3")
    public AjaxResult batchdel3(Long[] ids) {
        //dictService.deleteDictByIds(ids);
        System.out.println(ids);
        return AjaxResult.success();
    }
}
